package com.fan.shop.businesslogic;

import org.apache.log4j.Logger;

import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.domain.PurchaseOrder;
import com.fan.shop.domain.Stock;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.PurchaseLineService;
import com.fan.shop.services.PurchaseOrderService;
import com.fan.shop.services.StockService;

public class PurchaseOrderLineDeleteProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(PurchaseOrderLineDeleteProcess.class);
	private PurchaseLineService purchaseLineService;
	private PurchaseOrderService purchaseOrderService;
	private StockService stockService;
	private String poNumber;
	private String itemCode;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PurchaseOrderLineDeleteProcess.logger = logger;
	}

	public PurchaseLineService getPurchaseLineService() {
		return purchaseLineService;
	}

	public void setPurchaseLineService(PurchaseLineService purchaseLineService) {
		this.purchaseLineService = purchaseLineService;
	}

	public String getPoNumber() {
		return poNumber;
	}

	public void setPoNumber(String poNumber) {
		this.poNumber = poNumber;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	@Override
	public Integer processImpl() {
		if (purchaseLineService.hasPurchaseLine(poNumber, itemCode)) {
			PurchaseLine purchaseLine = purchaseLineService.getPurchaseLine(
					poNumber, itemCode);
			try {

				// purchaseLineService.deletePurchaseLine(purchaseLine);
				PurchaseOrder purchaseOrder = purchaseOrderService
						.getPurchaseOrder(poNumber);

				if (!stockService.hasStock(itemCode,
						purchaseOrder.getStoreCode())) {
					setErrorMessage("Stock: " + itemCode + " "
							+ purchaseOrder.getStoreCode() + " doesn't exist!");
					return ShopGlobalVariables.FAILED;
				} else {

					if (purchaseOrder.getStatus().equals(
							ShopGlobalVariables.ENTERING)) {
						Stock stock = stockService.getStock(itemCode,
								purchaseOrder.getStoreCode());
						stock.setHoldQty(stock.getHoldQty()
								- purchaseLine.getQuantity());
						stock.setQuantity(stock.getQuantity()
								+ purchaseLine.getQuantity());

						purchaseLineService.deletePurchaseLine(purchaseLine);
						stockService.updateStock(stock);
					} else {
						setErrorMessage("Purchase Order Line can only be deleted in "
								+ ShopGlobalVariables.ENTERING + " status!");
						return ShopGlobalVariables.FAILED;
					}
				}

			} catch (Exception e) {
				setErrorMessage(e.getMessage());
				e.printStackTrace();
				return ShopGlobalVariables.FAILED;
			}
			return ShopGlobalVariables.SUCCESS;
		} else {
			setErrorMessage("Purchase Order Line: " + poNumber + " " + itemCode
					+ " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
	}

}
